package com.sky.controller.admin;


import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传
 */
@RestController
@Slf4j
@Api(tags = "文件上传")
@RequestMapping("/admin/common")
public class CommonController {

    //注入阿里云工具类方法  1,属性注入  2构造器注入 3.setter注入
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result upload(MultipartFile file) throws IOException {  //MultipartFile

        //1.获取文件名(避免重复)  唯一uuid
        String originalFilename = file.getOriginalFilename();
        //1.1 获取后缀
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //1.2 UUID
        String newFileName = UUID.randomUUID().toString() + postfix;
        //                     上传                      新的文件名
        String url = aliOssUtil.upload(file.getBytes(), newFileName);
        return Result.success(url);

    }
}
